<?php

/**
 * This is the model class for table "research".
 *
 * The followings are the available columns in table 'research':
 * @property integer $id
 * @property integer $player_id
 * @property string $created
 * @property integer $status
 * @property integer $points
 * @property integer $sbe
 * @property integer $lag
 * @property integer $panz
 * @property integer $vba
 * @property integer $fusi
 * @property integer $zva
 * @property integer $rak
 * @property integer $lak
 * @property integer $pmw
 * @property integer $spio
 * @property integer $tarn
 * @property integer $kolo
 * @property integer $inva
 * @property integer $rec
 * @property integer $raid
 * @property integer $sabo
 *
 * The followings are the available model relations:
 * @property Player $player
 */
class Research extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Research the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'research';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('player_id, status, points, sbe, lag, panz, vba, fusi, zva, rak, lak, pmw, spio, tarn, kolo, inva, rec, raid, sabo', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, player_id, created, status, points, sbe, lag, panz, vba, fusi, zva, rak, lak, pmw, spio, tarn, kolo, inva, rec, raid, sabo', 'safe', 'on'=>'search'),
			array('created', 'safe'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'player' => array(self::BELONGS_TO, 'Player', 'player_id'),
		);
	}

  public function behaviors()
  {
    return array(
      'CTimestampBehavior' => array(
        'class' => 'zii.behaviors.CTimestampBehavior',
        'createAttribute' => 'created',
      )
    );
  }

  public function scopes()
  {
    return array(
      'active' => array(
        'condition' => 'status = 1',
      ),
    );
  }

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'player_id' => 'Player',
			'created' => 'Created',
			'status' => 'Status',
			'points' => 'Points',
			'sbe' => 'Sbe',
			'lag' => 'Lag',
			'panz' => 'Panz',
			'vba' => 'Vba',
			'fusi' => 'Fusi',
			'zva' => 'Zva',
			'rak' => 'Rak',
			'lak' => 'Lak',
			'pmw' => 'Pmw',
			'spio' => 'Spio',
			'tarn' => 'Tarn',
			'kolo' => 'Kolo',
			'inva' => 'Inva',
			'rec' => 'Rec',
			'raid' => 'Raid',
			'sabo' => 'Sabo',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('player_id',$this->player_id);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('points',$this->points);
		$criteria->compare('sbe',$this->sbe);
		$criteria->compare('lag',$this->lag);
		$criteria->compare('panz',$this->panz);
		$criteria->compare('vba',$this->vba);
		$criteria->compare('fusi',$this->fusi);
		$criteria->compare('zva',$this->zva);
		$criteria->compare('rak',$this->rak);
		$criteria->compare('lak',$this->lak);
		$criteria->compare('pmw',$this->pmw);
		$criteria->compare('spio',$this->spio);
		$criteria->compare('tarn',$this->tarn);
		$criteria->compare('kolo',$this->kolo);
		$criteria->compare('inva',$this->inva);
		$criteria->compare('rec',$this->rec);
		$criteria->compare('raid',$this->raid);
		$criteria->compare('sabo',$this->sabo);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

  public function savePoints($playerId, $points)
  {
    $research = new Research();
    $research->player_id = $playerId;
    $research->points = $points;
    if ($research->save()) {
      Yii::log('Forschungspunkte '.$research->points.' erfolgreich eingetragen für Player '.$playerId, CLogger::LEVEL_TRACE, 'sntools.model.research');
    }
  }
}